package com.huawei.M01.iterator.M06;

import java.util.HashMap;
import java.util.Map;

/**
 * 使字符串平衡的最小删除次数
 *
 * @author 111
 */
public class MiniMum {
    public int minimumDeletions(String s) {
        // 记录左右两边ab的个数
        int righta = 0;
        int leftb = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == 'a') {
                righta++;
            }
        }
        int res = righta;
        for (int i = 0; i < s.length(); i++) {
            // 先判断要不要结束循环
            if (s.charAt(i) == 'a') {
                righta--;
            }else {
                leftb++;
            }
            res = Math.min(res, leftb + righta);
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(new MiniMum().minimumDeletions("aababbab"));
    }
}
